package com.wilcom.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilcom.system.common.BaseResponse;
import com.wilcom.system.common.ErrorCode;
import com.wilcom.system.common.PageRequest;
import com.wilcom.system.common.ResultUtils;
import com.wilcom.system.domain.Repairrequest;
import com.wilcom.system.domain.Repairtask;
import com.wilcom.system.exception.ThrowUtils;
import com.wilcom.system.model.request.repairtask.*;
import com.wilcom.system.model.respone.RepairTaskDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/repairtask")
@Slf4j
public class RepairTaskController {
    @Resource
    private com.wilcom.system.service.RepairtaskService repairtaskService;

    @Resource
    private com.wilcom.system.service.RepairrequestService repairrequestService;

//     acceptRepairTask(taskId) - 接收报修任务。
    @PostMapping("/acceptRepairTask")
    public BaseResponse<Page<Repairrequest>> getAllRepairRequests(@RequestBody AcceptRepairTaskRequest postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Repairrequest> repairrequestPage = repairrequestService.page(new Page<>(current, size),
                repairtaskService.getQueryWrapper(postQueryRequest));
        return ResultUtils.success(repairrequestPage);
    }
//    - updateRepairTaskStatus(taskId, status) - 更新报修任务状态。
    @PostMapping("/updateRepairTaskStatus")
    public BaseResponse<Boolean> updateRepairTaskStatus(@RequestBody UpdateRepairTaskStatusRequest postQueryRequest) {
        Integer requestId = postQueryRequest.getRequestId();
        String status = postQueryRequest.getStatus();
        ThrowUtils.throwIf(status == null, ErrorCode.PARAMS_ERROR);
        Repairtask repairtask = new Repairtask();
        repairtask.setTaskId(requestId);
        repairtask.setStatus(status);
        return ResultUtils.success(repairtaskService.updateById(repairtask));
    }
//    - getAssignedTasks(technicianId) - 获取分配给特定维修人员的任务列表。
    @PostMapping("/getAssignedTasks")
    public BaseResponse<Page<Repairtask>> getAssignedTasks(@RequestBody GetAssignedTasksRequest postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Repairtask> repairtaskPage = repairtaskService.page(new Page<>(current, size),
                repairtaskService.getQueryWrapperTask(postQueryRequest));
        return ResultUtils.success(repairtaskPage);
    }
    @PostMapping("/getUserRepairTaskBy")
    public BaseResponse<List<RepairTaskDetail>> getUserRepairTaskBy(@RequestBody GetRepairTasksBy getRepairTasksBy) {
        long current = getRepairTasksBy.getCurrent();
        long size = getRepairTasksBy.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        List<RepairTaskDetail> repairtaskPage = repairtaskService.getUserRepairTaskBy(getRepairTasksBy);
        return ResultUtils.success(repairtaskPage);
    }
    @PostMapping("/getAssignedTasksDetailByUserId")
    public BaseResponse<List<RepairTaskDetail>> getAssignedTasksDetailByUserId(@RequestBody GetAssignedTasksRequest postQueryRequest) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        List<RepairTaskDetail> repairtaskPage =  repairtaskService.getAssignedTasksDetailByUserId(postQueryRequest);
        return ResultUtils.success(repairtaskPage);
    }

    // createRepairTask
    @PostMapping("/createRepairTasks")
    public BaseResponse<Boolean> createRepairTasks(@RequestBody CreateRepairTasks createRequest) {
         return ResultUtils.success(repairtaskService.createRepairTasks(createRequest));
    }
}
